package controllers.participant;

import java.util.Collection;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.EventService;
import services.ParticipantService;
import services.RegistrationService;
import controllers.AbstractController;
import domain.Event;
import domain.Registration;

@Controller
@RequestMapping("/event/participant")
public class EventParticipantController extends AbstractController {
	@Autowired
	private EventService eventService;

	@Autowired
	private ParticipantService participantService;
	@Autowired
	private RegistrationService registrationService;

	public EventParticipantController() {
		super();
	}
	//List
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView list() {
		ModelAndView result;
		Collection<Event> events;
		events = eventService.findEventsByParticipant();
		result = new ModelAndView("event/list");
		result.addObject("requestURI", "event/administrator/list.do");
		result.addObject("events", events);
		result.addObject("backURI", "/welcome/index.do");
		return result;
	} 
	
	@RequestMapping(value = "/ordered/list", method = RequestMethod.GET)
	public ModelAndView orderedList() {
		ModelAndView result;

		Collection<Event> events = eventService
				.findByParticipantOrderedByRating(participantService
						.findByPrincipal().getId());

		result = new ModelAndView("event/list");
		result.addObject("requestURI", "event/list.do");
		result.addObject("events", events);
		result.addObject("ordered", true);
		result.addObject("backURI", "/welcome/index.do");
		return result;
	}
	
	// View

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public ModelAndView edit(@RequestParam int eventId) {
		ModelAndView result;
		Integer registrationId = null;
		Date currentMoment = new Date();
		Event event;
		event = eventService.findOne(eventId);
		Boolean finished = event.getFinishMoment().compareTo(currentMoment) < 1;
		Integer participantId = participantService.findByPrincipal().getId();
		Registration registration = registrationService
				.getRegistrationForEvent(participantId, eventId);

		if (registrationService.getRegistrationForEvent(participantId, eventId) != null) {
			registrationId = registration.getId();
		}
		Boolean registered = registrationService.IsRegisteredForEvent(
				participantId, eventId);
		result = new ModelAndView("event/edit");
		result.addObject("event", event);
		result.addObject("registered", registered);
		result.addObject("registrationId", registrationId);
		result.addObject("requestURI", "event/participant/edit.do");
		result.addObject("finished", finished);
		result.addObject("isNew", false);
		result.addObject("backURI", "/welcome/index.do");

		return result;

	}
	
	// Ancillary methods

	protected ModelAndView createEditModelAndView(Event event) {
		ModelAndView result;
		result = createEditModelAndView(event, null);
		return result;

	}

	protected ModelAndView createEditModelAndView(Event event, String message) {
		ModelAndView result;

		result = new ModelAndView("event/edit");
		result.addObject("event", event);
		result.addObject("message", message);

		return result;

	}

}